% Master script that runs scripts in order.

thisFile = mfilename('fullpath');
thisDir  = fileparts(thisFile);
cd(thisDir);

% Ensure the folder (and optionally subfolders) are on the path
addpath(thisDir);

tStart = tic;

logfilename = 'LOG_MASTER.txt';
if (exist(logfilename)) 
    delete(logfilename);
end
diary(logfilename);

try
    fprintf('\n[1/N] Running m_commutecost.m ...\n');
    m_commutecost; 

    fprintf('[2/N] Running Main Calibration and Estimation ...\n');
    m_calibration_main; %
    m_calibration_center; %
    m_identificationtest; %  

    fprintf('[3/N] Running Robustness Check for Main Estimation: 10Grids; Spillovers; Diff. Parameters ...\n');
    m_calibration_robust_10grids; %
    m_calibration_spillover;  %
    m_calibration_robust_popdens36; %
    m_calibration_robust_highrho; %
    m_calibration_robust_lowrho; %
    m_calibration_AD; 
    m_calibration_AD_center; 
    
    fprintf('[4/N] Running Robustness Check for Main Calibration: Calibration for 1930s ...\n');
    m_calibration_1930s;
    
    fprintf('[5/N] Running Main Evaluation of 1945-50 Recovery ...\n');
    m_eval_1945_50; 

    fprintf('[6/N] Running Robustness Check for Evaluation of 1945-50 Recovery ...\n');
    m_eval_1945_50_noise; 
    m_eval_1945_50_robustness;
    m_calibration_noaggfund_1950; 

    fprintf('[7/N] Running Scale Check ...\n');
    m_scale;

    fprintf('[8/N] Running Counterfactuals-- m_no_agglomeration.m ...\n');
    m_no_agglomeration; 
    m_counterfactualpath; 
    m_welfare; 

    fprintf('[9/N] Additional Counterfactual Analysis -- Landowner, Myioic and Memory ...\n');
    m_landowner; 
    m_eval_1945_50_myopic; 
    m_eval_1945_50_memory; 

    fprintf('[10/N] Final Analysis for Counterfactuals --   ...\n');
    m_randomCBD; 

    fprintf('All steps completed successfully');

catch ME
    fprintf(2, '\nERROR: %s\n', ME.message);
    disp(getReport(ME, 'extended'));
    rethrow(ME); 
end

disp(['Elapsed time is: ', num2str(toc / 60), ' minutes']);
diary off; 